package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.PageResult;
import javafx.scene.shape.Circle;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 喜欢服务接口实现类
 */
@Service
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;



    /**
     * 根据当前用户id查询喜欢数量
     * @param userId
     * @return
     */
    @Override
    public Long findCountByUserId(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }


    /**
     * 根据当前用户id查询粉丝数量
     * @param userId
     * @return
     */
    @Override
    public Long findCountByLikeUserId(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }
    /**
     *互相喜欢
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageLikeEachOther(Long userId, int page, int pagesize) {
        //1.查询总记录数
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));//根据当前登录用户id作为条件 查询好友表
        long counts = mongoTemplate.count(query, Friend.class);
        //2.查询当前页面需要展示的数据
        query.limit((int)pagesize).skip((page-1)*pagesize);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (Friend friend : friendList) {
            Long friendId = friend.getFriendId();//根据好友id再查询recommendUser表
            recommendUserList.add(getRecommendUsers(userId, friendId));
        }
        //3.封装数据返回
        Long pages = counts/pagesize + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }


    /**
     * 我喜欢
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageOneSideLike(Long userId, int page, int pagesize) {
        //1.查询总记录数
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));//根据当前登录用户id作为条件 查询喜欢表
        long counts = mongoTemplate.count(query, UserLike.class);
        //2.查询当前页面需要展示的数据
        query.limit((int)pagesize).skip((page-1)*pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            Long friendId = userLike.getLikeUserId();//根据好友id再查询recommendUser表
            recommendUserList.add(getRecommendUsers(userId, friendId));
        }
        //3.封装数据返回
        Long pages = counts/pagesize + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 粉丝
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageFens(Long userId, int page, int pagesize) {
        //1.查询总记录数
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId));//根据当前登录用户id作为条件 查询喜欢表
        long counts = mongoTemplate.count(query, UserLike.class);
        //2.查询当前页面需要展示的数据
        query.limit((int)pagesize).skip((page-1)*pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            Long friendId = userLike.getUserId();//根据好友id再查询recommendUser表
            recommendUserList.add(getRecommendUsers(userId, friendId));
        }
        //3.封装数据返回
        Long pages = counts/pagesize + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 谁看过我
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageMyVisitors(Long userId, int page, int pagesize) {
        //1.查询总记录数
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));//根据当前登录用户id作为条件 查询访客表
        long counts = mongoTemplate.count(query, Visitor.class);
        //2.查询当前页面需要展示的数据
        query.limit((int)pagesize).skip((page-1)*pagesize);
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (Visitor visitor : visitorList) {
            Long friendId = visitor.getVisitorUserId();//根据好友id再查询recommendUser表
            recommendUserList.add(getRecommendUsers(userId, friendId));
        }
        //3.封装数据返回
        Long pages = counts/pagesize + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 粉丝-喜欢
     */
    @Override
    public void removeFansLike(Long fansUserId, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(fansUserId).and("likeUserId").is(userId));
        mongoTemplate.remove(query,UserLike.class);
    }

    /**
     * 添加粉丝
     * @param loveUserId
     * @param userId
     */
    @Override
    public void savefens(Long loveUserId, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("likeUserId").is(loveUserId));

        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setCreated(new Date().getTime());
        userLike.setLikeUserId(loveUserId);
        userLike.setUserId(userId);
        mongoTemplate.insert(userLike);
    }

    /**
     * 公共方法 查询recommendUser表
     */
    private RecommendUser getRecommendUsers(Long userId, Long friendId) {
        Query recommendUserQuery =  new Query();
        recommendUserQuery.addCriteria(Criteria.where("userId").is(friendId).and("toUserId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(recommendUserQuery, RecommendUser.class);
        if(recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(friendId);//非登录用户id(好友id 我喜欢的用户id 喜欢我的用户id 访客的用户id)
            recommendUser.setToUserId(userId);//登录用户id
            recommendUser.setScore(99d);//缘分值
            recommendUser.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            return recommendUser;
        }
        return recommendUser;
    }

}
